﻿/* QF classbuilder for Enlib */
using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Logging;

#region SYS_Position实体类
public class SYS_Position
{
	private string m_PositionID;
	private string m_Name;
	private string m_Description;
	private DateTime m_CreateTime;
	
	public SYS_Position ()
	{
		m_PositionID="";
		m_Name="";
		m_Description="";
		m_CreateTime=DateTime.Now;
	}
	
	public string PositionID
	{
		get{return m_PositionID;}
		set{m_PositionID=value;}
	}
	public string Name
	{
		get{return m_Name;}
		set{m_Name=value;}
	}
	public string Description
	{
		get{return m_Description;}
		set{m_Description=value;}
	}
	public DateTime CreateTime
	{
		get{return m_CreateTime;}
		set{m_CreateTime=value;}
	}
}
#endregion

#region SYS_Position管理类
public partial class LogicLayer
{
	public bool AddSYS_Position(SYS_Position theSYS_Position)
	{
		try
		{
			Database db = DatabaseFactory.CreateDatabase("connStr");
			string sql = "insert into SYS_Position (PositionID,Name,Description,CreateTime) values (@PositionID,@Name,@Description,@CreateTime)";
			System.Data.Common.DbCommand cmd = db.GetSqlStringCommand(sql);
			
			db.AddInParameter(cmd, "@PositionID", DbType.String, theSYS_Position.PositionID);
			db.AddInParameter(cmd, "@Name", DbType.String, theSYS_Position.Name);
			db.AddInParameter(cmd, "@Description", DbType.String, theSYS_Position.Description);
			db.AddInParameter(cmd, "@CreateTime", DbType.DateTime, theSYS_Position.CreateTime);
			
			db.ExecuteNonQuery(cmd);
			return true;
		}
		catch(Exception ex)
		{
			Logger.Write("AddSYS_Position:" + ex.Message );
			return false;
		}
	}
	
	public SYS_Position GetSYS_PositionFromDataReader(IDataReader dataReader)
	{
		try
		{
			SYS_Position result = new SYS_Position();
			
			if (dataReader["PositionID"] != null && dataReader["PositionID"] != System.DBNull.Value )
				result.PositionID = (string)dataReader["PositionID"];
			if (dataReader["Name"] != null && dataReader["Name"] != System.DBNull.Value )
				result.Name = (string)dataReader["Name"];
			if (dataReader["Description"] != null && dataReader["Description"] != System.DBNull.Value )
				result.Description = (string)dataReader["Description"];
			if (dataReader["CreateTime"] != null && dataReader["CreateTime"] != System.DBNull.Value )
				result.CreateTime = (DateTime)dataReader["CreateTime"];
			
			return result;
		}
		catch(Exception ex)
		{
			Logger.Write("GetReaderSYS_Position:" + ex.Message);
			return null;
		}
	}
	
	public SYS_Position GetSYS_Position(string PositionID)
	{
		try
		{
			Database db = DatabaseFactory.CreateDatabase("connStr");
			string sql = "select * From SYS_Position where PositionID=@PositionID";
			
			System.Data.Common.DbCommand cmd = db.GetSqlStringCommand(sql);
			db.AddInParameter(cmd, "@PositionID", DbType.String, PositionID);
			
			SYS_Position result = null;
			using (IDataReader dataReader = db.ExecuteReader(cmd))
			{
				if (dataReader.Read())
				{
					result = GetSYS_PositionFromDataReader( dataReader );
				}
			}
			return result;
		}
		catch(Exception ex)
		{
			Logger.Write("GetSYS_Position:" + ex.Message);
			return null;
		}
	}
	
	public bool DelSYS_Position(string PositionID)
	{
		try
		{
			Database db = DatabaseFactory.CreateDatabase("connStr");
			string sql = "delete From SYS_Position where PositionID=@PositionID";
			
			System.Data.Common.DbCommand cmd = db.GetSqlStringCommand(sql);
			db.AddInParameter(cmd, "@PositionID", DbType.String, PositionID);
			
			db.ExecuteNonQuery(cmd);
			return true;
		}
		catch(Exception ex)
		{
			Logger.Write("DelSYS_Position:" + ex.Message);
			return false;
		}
	}
	
	public bool UpdateSYS_Position(SYS_Position theSYS_Position)
	{
		try
		{
			Database db = DatabaseFactory.CreateDatabase("connStr");
			string sql = "update SYS_Position set Name=@Name,Description=@Description,CreateTime=@CreateTime where PositionID=@PositionID";
			System.Data.Common.DbCommand cmd = db.GetSqlStringCommand(sql);
			
			db.AddInParameter(cmd, "@Name", DbType.String, theSYS_Position.Name);
			db.AddInParameter(cmd, "@Description", DbType.String, theSYS_Position.Description);
			db.AddInParameter(cmd, "@CreateTime", DbType.DateTime, theSYS_Position.CreateTime);
			db.AddInParameter(cmd, "@PositionID", DbType.String, theSYS_Position.PositionID);
			
			db.ExecuteNonQuery(cmd);
			return true;
		}
		catch(Exception ex)
		{
			Logger.Write("UpdateSYS_Position:" + ex.Message);
			return false;
		}
	}
	
	public List<SYS_Position> ListSYS_Position()
	{
		try
		{
			Database db = DatabaseFactory.CreateDatabase("connStr");
			string sql = "select * from SYS_Position";
			System.Data.Common.DbCommand cmd = db.GetSqlStringCommand(sql);
			
			 List<SYS_Position> result = new  List<SYS_Position>();
			using (IDataReader dataReader = db.ExecuteReader(cmd))
			{
				while (dataReader.Read())
				{
					SYS_Position obj = GetSYS_PositionFromDataReader( dataReader );
					if(obj != null)
						result.Add(obj);
				}
			}
			return result;
		}
		catch(Exception ex)
		{
			Logger.Write("ListSYS_Position:" + ex.Message);
			return null;
		}
	}
	
}
#endregion